/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ProcessServlet;

import entity.bean.Movie;
import entity.bean.PurchaseRec;
import entity.bean.Show;
import entity.bean.Ticket;
import entity.connector.PurchaseUploader;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author 52571783
 */
public class Purchase extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        try {
            /*
             * TODO output your page here. You may use following sample code.
             */
            HttpSession session = request.getSession();
            String seatsMarked = request.getParameter("markedSeat");
            String[] eachSeatNo = seatsMarked.split(":");
            Ticket[] orderedTicket;
            Show selectedTimeSection = (Show) session.getAttribute("selectedTimeSection");

            //checking to prevent unselected seat for submission
            if (request.getParameter("markedSeat").equals("")) {
                RequestDispatcher dispatcher =
                        getServletContext().getRequestDispatcher("/purchase.jsp");
                dispatcher.forward(request, response);
            }
            //end of checking

            //getting input from the form of jsp
            seatsMarked = request.getParameter("markedSeat");
            int noOfSeatMarked = Integer.parseInt(request.getParameter("seatCount"));

            //initializing the ticket
            orderedTicket = new Ticket[noOfSeatMarked];
            for (int i = 0; i < noOfSeatMarked; i++) {
                orderedTicket[i] = new Ticket(selectedTimeSection, (int)(eachSeatNo[i].charAt(0) - 'A' + 1) ,new Integer(eachSeatNo[i].substring(1)));
            }
            //
            PurchaseRec purchaseRec = new PurchaseRec(noOfSeatMarked);
            PurchaseUploader purchaseUploader = PurchaseUploader.getInstance();
            
            for (int i = 0; i < noOfSeatMarked; i++) {
            purchaseUploader.purchaseTicket(orderedTicket[i]);
            }
            //end of getting input from form


            eachSeatNo = seatsMarked.split(":");



            //prepare for session checking
            //end of prepare
            
            session.setAttribute("purchaseRec", purchaseRec);
            session.setAttribute("purchaseUploader", purchaseUploader);
            session.setAttribute("orderedTicket", orderedTicket);
            session.setAttribute("eachSeatNo", eachSeatNo);
            session.setAttribute("noOfSeatMarked", (new Integer(noOfSeatMarked)));
            //passing parameter to javabeans



            RequestDispatcher dispatcher =
                    getServletContext().getRequestDispatcher("/payment.jsp");
            dispatcher.forward(request, response);
        } finally {
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
